/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author dtuanh
 */
import Dict.*;
import java.io.*;
import org.omg.CosNaming.*; // inclure le service de nommage
import org.omg.CORBA.*; // manipuler des objets CORBA
public class Client {
static Dictionnary helloImpl;
  public static void main(String args[])
    {
      /*
      try{
        // create and initialize the ORB
	ORB orb = ORB.init(args, null);

        // get the root naming context
        org.omg.CORBA.Object objRef =
	    orb.resolve_initial_references("NameService");
        // Use NamingContextExt instead of NamingContext. This is
        // part of the Interoperable naming Service.
        NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

        // resolve the Object Reference in Naming
        String name = "Dictionnary";
        helloImpl = DictionnaryHelper.narrow(ncRef.resolve_str(name));

        System.out.println("Obtained a handle on server object: " + helloImpl);
        System.out.println("Loading dict...");
        int n=helloImpl.loadDict();
        System.out.println("Dict size:"+n);
        n=helloImpl.loadDict();
        System.out.println("Dict size:"+n);
        Entity en= helloImpl.lastEntity();
       // System.out.println(en.myDefinition());

	} catch (Exception e) {
          System.out.println("ERROR : " + e) ;
	  e.printStackTrace(System.out);
	  }*/
 java.util.Properties props = System.getProperties();
    //props.put("org.omg.CORBA.ORBClass", "org.openorb.orb.core.ORB");
    //props.put("org.omg.CORBA.ORBSingletonClass",
    //          "org.openorb.orb.core.ORBSingleton");

    org.omg.CORBA.ORB orb = null;

     try {
         for (int i = 0; i < args.length; i++) {
             System.out.println("ARGS[" + i + "] = " + args[i]);
         }
           orb = org.omg.CORBA.ORB.init(args, props);
           //recuperer le service de nommage = objet CORBA
           org.omg.CORBA.Object objRef =
           orb.resolve_initial_references("NameService");
           // convertir l'objet CORBA `service de nommage' en objet Java
           NamingContext ncRef = NamingContextHelper.narrow(objRef);
           // preparer dans un objet composant le nom de l'objet CORBA
           // pas d'espace entre " et "
           NameComponent nc = new NameComponent("Dictionnary","dictionnary_context");
           NameComponent nc1 = new NameComponent("Dictionnary","Object");
           NameComponent [] path = {nc, nc1};
           NameComponent [] path2 = {nc1};
           //appeler le service de nommage pour qu'il trouve la reference
           // de notre objet CORBA
           // et convertir l'objet CORBA en objet Java
           objRef = ncRef.resolve(path);
           if (objRef == null) {
                System.out.println("Dictionnary not found !");
           }
           else {
               System.out.println("FUCK = " + objRef.getClass());
               System.out.println("THAG = " + objRef);
               Dictionnary dictRef = DictionnaryHelper.narrow (objRef);

                // faire appel a l'objet serveur et imprimer les resultats
                //String res = dictRef.reverseString(args[0]);
                //System.out.println("la chaine inversee de " + args[0]+ " est : " + res);
               java.io.DataInputStream input = new java.io.DataInputStream( System.in );
        int choice = 0;

        while(choice != 5){
            System.out.println("1. First entity\n");
            System.out.println("2. Next entity\n");
            System.out.println("3. Prevous entity\n");
            System.out.println("4. Last entity\n");
            System.out.println("5. Error logging\n");
            System.out.println("6. Quit\n");
            System.out.println("Your choice:");
            String s=input.readLine();
            choice=-1;
            try{
            choice = Integer.parseInt(s)    ;
            }
            catch(Exception ex){}
            
            Entity entity;
            dictRef.loadDict();
            switch(choice){
                case 1:
                    System.out.println("Load dict:"+dictRef.loadDict());
               
                    entity = dictRef.firstEntity();
                    System.out.println("pass 1");
                    
                    System.out.println(entity.myDefinition());
                    break;
                case 2:
                    System.out.println("Next");
                    break;
                case 3:
                    System.out.println("Previous");
                    break;
                case 4:
                    System.out.println("Last");
                    break;

                case 6:
                    break;
            }
        }




           }
      }
      catch(Exception e) {
           System.out.println("Erreur : " + e);
           e.printStackTrace(System.out);
      }


        // TODO code application logic here
    }

} // fin de la classe